//
//  SeePictureViewController.m
//  MyProject
//
//  Created by Pei on 16/7/23.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "SeePictureViewController.h"
#import "UIImageView+WebCache.h"

@interface SeePictureViewController ()<UIScrollViewDelegate>
{
    BOOL isFromStart;
    
    UIImageView *_currentImageView;
}

@property(nonatomic, strong)UIScrollView  *scrollView;      //声明一个UIScrollView
@property(nonatomic, strong)UIPageControl *pageControl;     //声明一个UIPageControl
@property(nonatomic, strong)NSArray  *arrayImages;          //存放图片的数组
@property(nonatomic, strong)NSMutableArray *viewController; //存放UIViewController的可变数组
@property(nonatomic, strong)NSTimer *timer;
@end

@implementation SeePictureViewController


- (id)initWithArray:(NSArray*)picA
{
    self = [super init];
    if (self) {
        self.arrayImages = picA;
    }
    return self;
}

- (void)viewDidLoad {
    self.navTitle = @"查看图片";
    [super viewDidLoad];
    
    //self.arrayImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"introduction_1"],[UIImage imageNamed:@"introduction_2"],[UIImage imageNamed:@"introduction_3"],[UIImage imageNamed:@"introduction_4"], nil];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth,kDeviceHeight-NAV_HEIGHT)];
    [_scrollView setPagingEnabled:YES];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    [_scrollView setDelegate:self];
    [_scrollView setBackgroundColor:kWhiteColor];
    
    //ContentSize 这个属性对于UIScrollView挺关键的，取决于是否滚动。
    [_scrollView setContentSize:CGSizeMake(kDeviceWidth * [self.arrayImages count], 0.0f)];
    //[_scrollView setContentSize:CGSizeMake(kDeviceWidth * [self.arrayImages count], kDeviceHeight-NAV_HEIGHT)];
    [self.view addSubview:_scrollView];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.scrollView.frame.size.height - 20, kDeviceWidth, 20)];
    [_pageControl setBackgroundColor:[UIColor clearColor]];
    
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = [self.arrayImages count];
    [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
    
    _viewController = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < [self.arrayImages count]; i++) {
        [_viewController addObject:[NSNull null]];
        
        [self loadScrollViewPage:i];
        
        //[self loadScrollViewPage:0];
        //[self loadScrollViewPage:1];
        //[self loadScrollViewPage:2];
        //[self loadScrollViewPage:3];
        
    }
    
    //_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollPages) userInfo:nil repeats:YES];
    
    
}


-(void)loadScrollViewPage:(NSInteger)page
{
    if (page >= self.arrayImages.count) {
        return;
    }
    
    UIViewController *imageViewController = [self.viewController objectAtIndex:page];
    if ((NSNull *)imageViewController == [NSNull null])
    {
        imageViewController = [[UIViewController alloc] init];
        
        [self.viewController replaceObjectAtIndex:page withObject:imageViewController];
    }
    
    if (imageViewController.view.superview == nil) {
        CGRect frame = self.scrollView.frame;
        frame.origin.x = CGRectGetWidth(frame) * page;
        frame.origin.y = 0;
        imageViewController.view.frame = frame;
        
        //[self addChildViewController:imageViewController];
        [self.scrollView addSubview:imageViewController.view];
        [imageViewController didMoveToParentViewController:self];
        
        UIImageView *tempImageView = [[UIImageView alloc] init];
        tempImageView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
        //tempImageView.image = (UIImage *)[self.arrayImages objectAtIndex:page];
        [tempImageView sd_setImageWithURL:[self.arrayImages objectAtIndex:page] placeholderImage:[UIImage imageNamed:@"normalDefault"]];
        _currentImageView = tempImageView;
        [imageViewController.view addSubview:tempImageView];
        
        //[imageViewController.view setBackgroundColor:[UIColor colorWithPatternImage:(UIImage *)[self.arrayImages objectAtIndex:page]]];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
    NSInteger page = floor((self.scrollView.contentOffset.x -pageWidth/2)/pageWidth) +1;
    self.pageControl.currentPage = page;
    
    [self loadScrollViewPage:page-1];
    [self loadScrollViewPage:page];
    [self loadScrollViewPage:page+1];
}

- (void)changePage:(id)sender
{
    NSInteger page = self.pageControl.currentPage;
    
    [self loadScrollViewPage:page - 1];
    [self loadScrollViewPage:page];
    [self loadScrollViewPage:page + 1];
    
    CGRect bounds = self.scrollView.bounds;
    bounds.origin.x = CGRectGetWidth(bounds) * page;
    bounds.origin.y = 0;
    [self.scrollView scrollRectToVisible:bounds animated:YES];
}

//告诉scrollview要缩放的是哪个子控件
 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
 {
    return _currentImageView;
}



-(void)scrollPages{
    ++self.pageControl.currentPage;
    CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
    if (isFromStart) {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        self.pageControl.currentPage = 0;
    }
    else
    {
        [self.scrollView setContentOffset:CGPointMake(pageWidth*self.pageControl.currentPage, self.scrollView.bounds.origin.y)];
        
    }
    if (_pageControl.currentPage == _pageControl.numberOfPages - 1) {
        isFromStart = YES;
    }
    else
    {
        isFromStart = NO;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
